package com.njxzc.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njxzc.common.http.ResponseResult;
import com.njxzc.common.security.utiles.MyUtil;
import com.njxzc.entity.CartEntity;
import com.njxzc.entity.FocusEntity;
import com.njxzc.entity.GoodsEntity;
import com.njxzc.mapper.admin.GoodsMapper;
import com.njxzc.mapper.before.FocusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public ResponseResult<Map<String, Object>> getGoods(GoodsEntity goodsEntity) {
        Integer pageNum = goodsEntity.getCurrentPage();
        if (pageNum == null || pageNum < 0)
            goodsEntity.setCurrentPage(1);
        IPage<GoodsEntity> iPage = new Page<>(goodsEntity.getCurrentPage(), 5);
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer goodsTypeId = goodsEntity.getGoodstypeId();
        String gName = goodsEntity.getGname();
        if (goodsTypeId != null && goodsTypeId != 0)
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodsTypeId);
        if (gName != null && !gName.isEmpty())
            lambdaQueryWrapper.like(GoodsEntity::getGname, gName);
        IPage<GoodsEntity> page = goodsMapper.iPageGoods(iPage, lambdaQueryWrapper);
        HashMap<String, Object> myRes = new HashMap<>();
        myRes.put("allGoods", page.getRecords());
        myRes.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(myRes);
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getAdvGoods() {
        List<GoodsEntity> listAdv = lambdaQuery().eq(GoodsEntity::getIsAdvertisement, 1)
                .orderByDesc(GoodsEntity::getId).last("limit 5").list();
        return ResponseResult.getSuccessResult(listAdv);
    }

    public ResponseResult<GoodsEntity> getGoodsById(GoodsEntity goodsEntity) {
        return ResponseResult.getSuccessResult(getById(goodsEntity.getId()));
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getGoodsIndex(GoodsEntity goodsEntity) {
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer goodsTypeId = goodsEntity.getGoodstypeId();
        String gName = goodsEntity.getGname();
        if (goodsTypeId != null && goodsTypeId != 0) {
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodsTypeId);
        }
        if (gName != null && !gName.isEmpty()) {
            String trimGName = gName.trim();
            lambdaQueryWrapper.like(GoodsEntity::getGname, trimGName);
        }
        lambdaQueryWrapper.orderByDesc(GoodsEntity::getId).last("limit 15");
        List<GoodsEntity> listIndex = list(lambdaQueryWrapper);
        return ResponseResult.getSuccessResult(listIndex);
    }

    @Override
    public ResponseResult<Map<String, String>> addGoods(GoodsEntity goodsEntity) {
        byte[] logoFile = goodsEntity.getLogoFile();
        if (logoFile != null && logoFile.length > 0) {
            String path = "D:\\ebusiness-vue\\ebusiness-vue\\src\\assets";
            String path_linux = "/var/www/ebusiness-vue/src/assets";
            String fileName = MyUtil.getNewFileName(goodsEntity.getFileName());
            File filePath = new File(path + File.separator + fileName);
            if (!filePath.getParentFile().exists()) {
                filePath.getParentFile().mkdirs();
            }
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(filePath);
                fileOutputStream.write(logoFile);

            } catch (IOException e) {
                throw new RuntimeException(e);

            }
            goodsEntity.setGpicture(fileName);
        }
        if("add".equals(goodsEntity.getAct())){
            boolean result= save(goodsEntity);
            if(result){
                return ResponseResult.getMessageResult(null,"A001");
            }
            else{
                return ResponseResult.getMessageResult(null,"A002");
            }
        }else{
            boolean result =updateById(goodsEntity);
            if(result){
                return ResponseResult.getMessageResult(null,"A001");
            }else{
                return ResponseResult.getMessageResult(null,"A002");
            }
        }
    }

    @Override
    public ResponseResult<List<GoodsEntity>> deleteGoods(GoodsEntity goodsEntity) {
        long res1=0;
        long res2=0;
        long res3=0;
        if(res1>0||res2>0||res3>0){ //不能删除
            return ResponseResult.getMessageResult(null,"A001");
        }
        if(removeById(goodsEntity)){
            return ResponseResult.getMessageResult(null,"A002");
        }
        else{
            return ResponseResult.getMessageResult(null,"A004");
        }
    }

    @Override
    public ResponseResult<List<Map<String, Object>>> myCarGoods(CartEntity cartEntity) {
         List<Map<String,Object>> list=goodsMapper.myCartGoods(cartEntity.getBusertableId());

        return ResponseResult.getSuccessResult(list);
    }


    @Override
    public ResponseResult<Map<String, Object>> iPageMyFocusGoods(GoodsEntity goodsEntity) {
        IPage<GoodsEntity> iPage = new Page<>(goodsEntity.getCurrentPage(),5);
        LambdaQueryWrapper<FocusEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FocusEntity::getBusertableId,goodsEntity.getBusertableId());
        IPage<GoodsEntity> page= goodsMapper.iPageMyFocusGoods(iPage,lambdaQueryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("allGoods",page.getRecords());
        map.put("totalPage",page.getPages());
        return ResponseResult.getSuccessResult(map);
    }
}
